package com.ruoyi.mateRequistion.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateProcurement.domain.MateProcurement;
import com.ruoyi.mateProcurement.service.MateProcurementService;
import com.ruoyi.mateRequistion.domain.KeepSample;
import com.ruoyi.mateRequistion.domain.ManageInput;
import com.ruoyi.mateRequistion.domain.ManageReviewInformation;
import com.ruoyi.mateRequistion.domain.ManageSign;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.domain.ProductRetention;
import com.ruoyi.mateRequistion.domain.SampleHandle;
import com.ruoyi.mateRequistion.domain.SampleRequistion;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateRequistion.service.MateRequistionService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.service.MateWarehousingService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateRequistion")
public class MateRequistionController extends BaseController {

    @Autowired
    MateRequistionService mateRequistionService;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateProcurementService mateProcurementService;

    @Autowired
    MateWarehousingService mateWarehousingService;

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    /**
     * 新增保存领料单
     */
    @PostMapping("/addMateRequistion")
    public AjaxResult addMateRequistion(@RequestBody MateRequistion mateRequistion) {
        mateRequistion.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addMateRequistion(mateRequistion);
        if (a > 0) {
            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(mateRequistion.getRequistionId());
            mtAudit.setZjName(mateRequistion.getDeptPerson());
            mtAudit.setOrderType("5");
            mtAudit.setOrderNum(mateRequistion.getRequistionOrderNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @PostMapping("/updateMateRequistion")
    public AjaxResult updateMateRequisition(@RequestBody MateRequistion mateRequistion) {
        mateRequistion.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.updateMateRequistion(mateRequistion);
        if (a > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @PostMapping("/addManageInput")
    public AjaxResult addManageInput(@RequestBody ManageInput manageInput) {
        manageInput.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addManageInput(manageInput);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addManageReviewInformation")
    public AjaxResult addManageReviewInformation(@RequestBody ManageReviewInformation manageReviewInformation) {
        manageReviewInformation.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionMapper.addManageReviewInformation(manageReviewInformation);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addManageSign")
    public AjaxResult addManageSign(@RequestBody ManageSign manageSign) {
        manageSign.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addManageSign(manageSign);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addProductRetention")
    public AjaxResult addProductRetention(@RequestBody ProductRetention productRetention) {
        productRetention.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addProductRetention(productRetention);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }


    @GetMapping("/productRetentionList")
    @ResponseBody
    public TableDataInfo productRetentionList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                              @RequestParam(value = "endTime", required = false) String endTime) {

        ProductRetention productRetention = new ProductRetention();
        productRetention.setBeginTime(beginTime);
        productRetention.setEndTime(endTime);
        startPage();
        productRetention.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ProductRetention> list = mateRequistionMapper.productRetentionList(productRetention);
        return getDataTable(list);
    }


    @GetMapping("/selectProductRetention")
    public ProductRetention selectProductRetention(@RequestParam(value = "retentionId", required = false) Long retentionId) {
        ProductRetention productRetention = mateRequistionMapper.selectProductRetention(retentionId);
        return productRetention;
    }

    @PostMapping("/addSampleRequisition")
    public AjaxResult addSampleRequisition(@RequestBody SampleRequistion sampleRequistion) {
        sampleRequistion.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addSampleRequisition(sampleRequistion);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @PostMapping("/addKeepSample")
    public AjaxResult addKeepSample(@RequestBody KeepSample keepSample) {
        keepSample.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addKeepSample(keepSample);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @GetMapping("/keepSampleList")
    @ResponseBody
    public TableDataInfo keepSampleList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        KeepSample keepSample = new KeepSample();
        keepSample.setBeginTime(beginTime);
        keepSample.setEndTime(endTime);
        startPage();
        keepSample.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<KeepSample> list = mateRequistionMapper.keepSampleList(keepSample);
        return getDataTable(list);
    }

    @GetMapping("/sampleRequistionList")
    @ResponseBody
    public TableDataInfo sampleRequistionList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        SampleRequistion sampleRequistion = new SampleRequistion();
        sampleRequistion.setBeginTime(beginTime);
        sampleRequistion.setEndTime(endTime);
        startPage();
        sampleRequistion.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<SampleRequistion> list = mateRequistionMapper.sampleRequistionList(sampleRequistion);
        return getDataTable(list);
    }


    @GetMapping("/selectKeepSample")
    public KeepSample selectKeepSample(@RequestParam(value = "keepId", required = false) Long keepId) {
        KeepSample keepSample = mateRequistionMapper.selectKeepSample(keepId);
        return keepSample;
    }


    @GetMapping("/selectSampleRequistion")
    public SampleRequistion selectSampleRequistion(@RequestParam(value = "sampleId", required = false) Long sampleId) {
        SampleRequistion sampleRequistion = mateRequistionMapper.selectSampleRequistion(sampleId);
        return sampleRequistion;
    }


    @PostMapping("/addSampleHandle")
    public AjaxResult addSampleHandle(@RequestBody SampleHandle sampleHandle) {
        sampleHandle.setDeptId(SecurityUtils.getDeptId());
        int a = mateRequistionService.addSampleHandle(sampleHandle);
        if (a > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败,请联系技术员");
        }
    }

    @GetMapping("/SampleHandleList")
    @ResponseBody
    public TableDataInfo SampleHandleList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                          @RequestParam(value = "endTime", required = false) String endTime) {

        SampleHandle sampleHandle = new SampleHandle();
        sampleHandle.setBeginTime(beginTime);
        sampleHandle.setEndTime(endTime);
        startPage();
        sampleHandle.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<SampleHandle> list = mateRequistionMapper.sampleHandleList(sampleHandle);
        return getDataTable(list);
    }

    @GetMapping("/selectSampleHandle")
    public SampleHandle selectSampleHandle(@RequestParam(value = "sampleHandleId", required = false) Long sampleHandleId) {
        SampleHandle sampleHandle = mateRequistionMapper.selectSampleHandle(sampleHandleId);
        return sampleHandle;
    }

    @GetMapping("/manageSignList")
    @ResponseBody
    public TableDataInfo manageSignList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {

        ManageSign manageSign = new ManageSign();
        manageSign.setBeginTime(beginTime);
        manageSign.setEndTime(endTime);
        startPage();
        manageSign.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ManageSign> list = mateRequistionMapper.manageSignList(manageSign);
        return getDataTable(list);
    }

    @GetMapping("/selectManageSign")
    public ManageSign selectManageSign(@RequestParam(value = "manageSignId", required = false) Long manageSignId) {
        ManageSign manageSign = mateRequistionMapper.selectManageSign(manageSignId);
        return manageSign;
    }

    @GetMapping("/manageInputList")
    @ResponseBody
    public TableDataInfo manageInputList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "endTime", required = false) String endTime) {

        ManageInput manageInput = new ManageInput();
        manageInput.setBeginTime(beginTime);
        manageInput.setEndTime(endTime);
        startPage();
        manageInput.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ManageInput> list = mateRequistionMapper.manageInputList(manageInput);
        return getDataTable(list);
    }

    @GetMapping("/reviewInformationList")
    @ResponseBody
    public TableDataInfo reviewInformationList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                               @RequestParam(value = "endTime", required = false) String endTime) {
        ManageReviewInformation manageReviewInformation = new ManageReviewInformation();
        manageReviewInformation.setBeginTime(beginTime);
        manageReviewInformation.setEndTime(endTime);
        startPage();
        manageReviewInformation.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<ManageReviewInformation> list = mateRequistionMapper.reviewInformationList(manageReviewInformation);
        return getDataTable(list);
    }

    @GetMapping("/selectManageReviewInformation")
    public ManageReviewInformation selectManageReviewInformation(@RequestParam(value = "informationId", required = false) Long informationId) {
        ManageReviewInformation manageReviewInformation = mateRequistionMapper.selectManageReviewInformation(informationId);
        return manageReviewInformation;
    }


    @GetMapping("/selectManageInput")
    public ManageInput selectManageInput(@RequestParam(value = "inputId", required = false) Long inputId) {
        ManageInput manageInput = mateRequistionMapper.selectManageInput(inputId);
        return manageInput;
    }


    /*
    查询所有的原材料信息
     */
    @GetMapping("/mateProductList")
    @ResponseBody
    public List<MateWarehousing> mateProductList(@RequestParam(value = "productType", required = false) String productType,
                                                 @RequestParam(value = "productName", required = false) String productName) {

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("productType", productType);
        resMap.put("productName", productName);
        resMap.put("deptId", SecurityUtils.getDeptId());
        List<MateWarehousing> list = mateWarehousingService.mateProductList(resMap);
        return list;
    }

    /*
    查询所有的领料单
     */
    @GetMapping("/requistionList")
    @ResponseBody
    public TableDataInfo requistionList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                        @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        MateRequistion mateRequistion = new MateRequistion();
        mateRequistion.setBeginTime(beginTime);
        mateRequistion.setEndTime(endTime);

        mateRequistion.setDeptId(SecurityUtils.getDeptId());
        List<MateRequistion> list = mateRequistionService.requistionList(mateRequistion);
        return getDataTable(list);

    }

    @GetMapping("/selectEdit")
    @ResponseBody
    public Map<String, Object> selectEdit(@RequestParam(value = "requistionId", required = false) Long requistionId) {
        Map<String, Object> resMap = new HashMap<>();
        MateRequistion mateRequistion = mateRequistionService.selectMateRequistion(requistionId);
        resMap.put("result", mateRequistion);
        return resMap;
    }

    /*
    查询审批完成的入库单
     */
    @GetMapping("/selectProcurement")
    @ResponseBody
    public Map<String, Object> selectProcurement() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateProcurement> MateProcurement = mateProcurementService.selectProcurement(SecurityUtils.getDeptId());
        resMap.put("result", MateProcurement);
        return resMap;
    }

    @GetMapping("/remove/{requistionId}")
    @ResponseBody
    public AjaxResult removeMateRequistion(@PathVariable("requistionId") Long requistionId) {
        mateRequistionService.removeMateRequistion(requistionId);
        return AjaxResult.success("删除成功");
    }

}
